package com.openget.lang;

import org.w3c.dom.*;
import com.openget.util.*;

public abstract class ElementModel {

	private Element sourceElement;	

    public Element getSourceElement() {
        return this.sourceElement;
    }
    
    public ElementModel() {
    }
    
    public ElementModel(Element source) {
    	this.sourceElement = source;
    }
	
	public void execute(OpengetContext context)
    {
    	if (this instanceof IFatherElement) {
    		Node singleton = this.getSourceElement().getFirstChild();
    		if (singleton.getNodeType() == org.w3c.dom.Node.TEXT_NODE || 
    			singleton.getNodeType() == org.w3c.dom.Node.CDATA_SECTION_NODE)
    			((IFatherElement)this).bastard(context);
    	}
    	
    	ElementHelper.execute(ElementLocator.getConditions(this), context);
    	
    	if (this instanceof IStrategyElement)
	    	((IStrategyElement)this).implementation(context);
    	
    	if (this instanceof IMultiplicationElement) {
    		final ElementModel element = this;
    		ElementHelper.into(this, ((IMultiplicationElement)this).multiply(context));
    		((IMultiplicationElement)this).multiplyAsync(context, new IDelegate<OpengetContext[]>() {
    			public void invoke(OpengetContext[] contexts) {
    				ElementHelper.into(element, contexts);
    			}
    		});
    	}
    	else
    		ElementHelper.into(this, context);
    }
}
